package com.tql.mall.boss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tql.mall.boss.mapper.CategoryThreeMapper;
import com.tql.mall.boss.service.CategoryThreeService;
import com.tql.mall.boss.service.CategoryTwoService;
import com.tql.mall.model.enums.category.OperFailEnum;
import com.tql.mall.model.product.BaseCategory3;
import com.tql.mall.model.product.level.BaseLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CategoryThreeServiceImpl extends ServiceImpl<CategoryThreeMapper, BaseCategory3> implements CategoryThreeService {
    @Autowired
    private CategoryTwoService categoryTwoService;

    /**
     * 根据id删除三级项
     * @param id
     * @return
     */
    @Override
    public String deleteById(Long id) {
        BaseCategory3 baseCategory3 = baseMapper.selectById(id);
        if (baseCategory3 == null) return OperFailEnum.DELETE_FAIL_ID_NULL.getMsg();
        baseMapper.deleteById(id);
        return null;
    }
    /**
     * 判断 三级项中是否存在依赖category2_id为 id的项
     * @param id
     * @return
     */
    @Override
    public boolean isExistRely(Long id) {
        QueryWrapper<BaseCategory3> baseCategory3QueryWrapper = new QueryWrapper<>();
        baseCategory3QueryWrapper.eq("category2_id",id);
        List<BaseCategory3> list = baseMapper.selectList(baseCategory3QueryWrapper);
        if (list == null || list.size() == 0) return false;
        return true;
    }
    /**
     * 根据二级分类中的id，获取List<BaseLevel>
     * @param id
     * @return
     */
    @Override
    public List<BaseLevel> getBaseLevelList(Long id) {
        QueryWrapper<BaseCategory3> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category2_id",id);
        List<BaseCategory3> list = baseMapper.selectList(queryWrapper);
        List<BaseLevel> resList = new ArrayList<>();
        for (BaseCategory3 baseCategory3: list){
            BaseLevel tmpBaseLevel = new BaseLevel();
            tmpBaseLevel.setCategoryId(baseCategory3.getId());
            tmpBaseLevel.setCategoryName(baseCategory3.getName());
            tmpBaseLevel.setCategoryChild(null);
            resList.add(tmpBaseLevel);
        }
        return resList;
    }
    @Override
    public String updateCategory(Long id, String newName, Long selectedCategory2Id) {
        BaseCategory3 baseCategory3 = baseMapper.selectById(id);
        if (baseCategory3.getName().equals(newName) && baseCategory3.getCategory2Id().equals(selectedCategory2Id)){//名字和上级都没改
            return null;
        }
        if (!baseCategory3.getCategory2Id().equals(selectedCategory2Id)){
            baseCategory3.setCategory2Id(selectedCategory2Id);
        }
        //判断是否存在重名
        //查看新的二级分类下的三级分类是否存在与newName重名的情况
        QueryWrapper<BaseCategory3> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category2_id",baseCategory3.getCategory2Id())
                .eq("name",newName);
        BaseCategory3 category3 = baseMapper.selectOne(queryWrapper);
        if (category3 != null){ //有重名的情况
            return OperFailEnum.EDIT_FILE_NAME_DEPLICATED.getMsg();
        }
        baseCategory3.setName(newName);
        //执行更新
        baseCategory3.setUpdateTime(new Date());
        baseMapper.updateById(baseCategory3);
        return null;
    }

    @Override
    public List<BaseCategory3> getCategoryListWithSameParent(Long id) {
        QueryWrapper<BaseCategory3> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category2_id",id);
        return baseMapper.selectList(queryWrapper);
    }
    /**
     * 插入三级项
     * @param baseCategory3
     * @return
     */
    @Override
    public String saveLevelThree(BaseCategory3 baseCategory3) {
        //分类名称为空
        if (StringUtils.isEmpty(baseCategory3.getName()))
            return OperFailEnum.ADD_FAIL_NAME_NULL.getMsg();
        //上级编号不存在
        if (baseCategory3.getCategory2Id() == null || !categoryTwoService.isExist(baseCategory3.getCategory2Id()))
            return OperFailEnum.ADD_FAIL_UPID_NULL.getMsg();
        baseMapper.insert(baseCategory3);
        Long id = baseCategory3.getId();
        System.out.println(id);
        return null;
    }
}
